package com.one.common.manager.imagchoose;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.FragmentActivity;

import com.imnjh.imagepicker.CapturePhotoHelper;
import com.imnjh.imagepicker.PickerConfig;
import com.imnjh.imagepicker.SImagePicker;
import com.imnjh.imagepicker.activity.CropImageActivity;
import com.one.common.CommonApp;
import com.one.common.manager.cache.CacheManager;
import com.one.common.utils.Toaster;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;

import io.reactivex.functions.Consumer;

import static android.Manifest.permission.READ_EXTERNAL_STORAGE;

/**
 * Author：zhulunjun
 * Time：2017/10/9
 * description：https://github.com/martin90s/ImagePicker 图片轮子
 */

public class ImagePickerChoose implements IChooseImg {


    public ImagePickerChoose() {
        // 初始化
        SImagePicker.init(
                new PickerConfig.Builder()
                        .setAppContext(CommonApp.getInstance())
                        .setImageLoader(new ChooseGlideLoader())
                        .setToolbaseColor(Color.BLACK)
                        .setAvatarPath(CacheManager.getInstance().getImageInnerCache()
                                .getAbsolutePath(AVATAR_FILE_NAME))
                        .build());
    }

    @SuppressLint("CheckResult")
    @Override
    public void chooseSingle(final Context context) {
        RxPermissions rxPermissions = new RxPermissions((FragmentActivity) context);
        rxPermissions.request(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) { // 在android 6.0之前会默认返回true
                            SImagePicker
                                    .from((Activity) context)
                                    .pickMode(SImagePicker.MODE_AVATAR)
                                    .showCamera(true) // 不显示相机
                                    .setCrop(false) // 不剪切
                                    .forResult(REQUEST_CODE_AVATAR);
                        } else {
                            // 未获取权限
                            Toaster.showShortToast("您没有授权该权限，请在设置中打开授权");
                        }
                    }
                });
    }


    @Override
    public void chooseMultiple(Context context, int multi) {
        chooseMultiple(context, multi, null);
    }

    @SuppressLint("CheckResult")
    @Override
    public void chooseMultiple(final Context context, final int multi, final ArrayList<String> selects) {
        RxPermissions rxPermissions = new RxPermissions((FragmentActivity) context);
        rxPermissions.request(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) { // 在android 6.0之前会默认返回true
                            SImagePicker
                                    .from((Activity) context)
                                    .maxCount(multi)
                                    .rowCount(ROW_COUNT)
                                    .showCamera(true) // 不显示相机
                                    .setSelected(selects)
                                    .pickMode(SImagePicker.MODE_IMAGE)
                                    .forResult(REQUEST_CODE_IMAGE);
                        } else {
                            // 未获取权限
                            Toaster.showShortToast("您没有授权该权限，请在设置中打开授权");
                        }
                    }
                });

    }

    @SuppressLint("CheckResult")
    @Override
    public void photograph(final Context context) {
        RxPermissions rxPermissions = new RxPermissions((FragmentActivity) context);
        rxPermissions.request(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) { // 在android 6.0之前会默认返回true
                            new CapturePhotoHelper((Activity) context).capture();
                        } else {
                            // 未获取权限
                            Toaster.showShortToast("您没有授权该权限，请在设置中打开授权");
                        }
                    }
                });
    }

    @SuppressLint("CheckResult")
    @Override
    public void photographWithCrop(final Context context) {
        RxPermissions rxPermissions = new RxPermissions((FragmentActivity) context);
        rxPermissions.request(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) { // 在android 6.0之前会默认返回true
                            new CapturePhotoHelper((Activity) context).capture(true);
                        } else {
                            // 未获取权限
                            Toaster.showShortToast("您没有授权该权限，请在设置中打开授权");
                        }
                    }
                });

    }

    @Override
    public void cropPicture(Context context, String path) {
        CropImageActivity.startImageCrop((Activity) context, path,
                REQUEST_CODE_CROP_IMAGE, CropImageActivity.PARAM_AVATAR_PATH);


    }


}
